<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Test extends MY_Controller
{
	public function __construct()
	{
        parent::__construct();
        $this->load->model(['site_model','sitedisapproved_model']);
    }


    private function getservice($site)
    {
        //$config=json_decode($site["extra"],true);
        //$apiauth=$config["apiauth"];
        //file_put_contents("./api.json",$apiauth);
        putenv('GOOGLE_APPLICATION_CREDENTIALS=./api.json');
        $client = new Google_Client();
        $client->setApplicationName("My Application");
        $client->useApplicationDefaultCredentials();      	
        $client->addScope(Google_Service_ShoppingContent::CONTENT);        
        $service = new Google_Service_ShoppingContent($client);
        return $service;       
    }

    public function index()
    {
        $siteid=34;
        $itemcount = 0;
        $site=$this->site_model->getitembyid($siteid);
		if($site)
		{
            //$config=json_decode($site["extra"],true);
            //$merchantId=$config["merchid"];
            $merchantId = "141628943";
            $service=$this->getservice($site);
            if($service && $merchantId)
            {            
                $products = $service->productstatuses->listProductstatuses($merchantId);
                $parameters = array();
                while (!empty($products->getResources())) {
                    foreach ($products->getResources() as $product) {
                        $fe = false;
                        $newrecord=[];
                        $error = $product->getItemLevelIssues();
                        foreach($error as $e)
                        {
                        	if($e["servability"]=="disapproved") 
                        	{
                        		$fe=true;
                        		$newrecord["code"]=$e["code"];
                        		$newrecord["skuid"]=$product->getProductId();
                        		$newrecord["siteid"]=$siteid;
                        		$tmp=explode("_",$newrecord["skuid"]);
                        		$newrecord["prid"]=$tmp[1];
                        		$newrecord["title"]=$product->getTitle();
                        		$newrecord["link"]=$product->getLink();
                        		break;
                        	}
                        }
                        if($fe)
                        {
	                        printf("%s\n", $product->getProductId());
                        	//print_r($error);
                        	$this->sitedisapproved_model->create($newrecord);
                        }
                        $itemcount++;
                    }
                    
                    if (empty($products->getNextPageToken())) {
                        break;
                    }
                    echo "fetch next page ".$parameters['pageToken']."\n";
                    $parameters['pageToken'] = $products->nextPageToken;
                    $products = $service->productstatuses->listProductstatuses($merchantId, $parameters);
                }
            }
        }
		echo $itemcount;
        
        die();
    }
}
